// 合约接口
import request from "@/request";
import { httpJson,httpMultipart } from "@/request/http";

 // 获取可用余额
export const _getBalance = () => {
    return request({
        url: "/api/wallet!getUsdt.action",
        method: "GET",
        // isLoading: true
    })
};


// 获取深度深度数据
export const _getDeepData = (symbol) => {
    return request({
        url: '/api/hobi!getDepth.action',
        method: 'GET',
        params: {
            symbol
        }
    })
}


/** =============================  永续合约委托 =======================*/

// 开仓初始化参数
export const _initOpen =  (symbol) => {
    return request({
        url: '/api/contractApplyOrder!openview.action',
        method: 'GET',
        params: {
            symbol
        }
    })
}

// 永续合约开仓
export const _orderOpen = (params) => {
    return httpJson({
        url: '/api/contractApplyOrder!open.action',
        method: 'GET',
        isLoading: true
    }, params)
}

// 平仓初始化参数
export const _initClose =  (symbol) => {
    return request({
        url: '/api/contractApplyOrder!closeview.action',
        method: 'GET',
        params: {
            symbol
        }
    })
}

// 永续合约平仓
export const _orderClose = (params) => {
    return request({
        url: '/api/contractApplyOrder!close.action',
        method: 'GET',
        loading: true,
        params: {
            symbol: params.symbol,
            session_token: params.session_token,
            direction: params.direction || 'buy', // buy, sell
            amount: params.amount,  // 数量
            price_type: params.price_type || 'opponent', // limit - 限价，opponent - 市价
            price: params.price, // limit price
            lever_rate: params.lever_rate || 1 // 杠杆
        }
    })
}

/** 当前委托 */

// 当前委托列表
export const _orderListCur =  (symbol, type = 'orders', page_no = 1) => {
    return request({
        url: '/api/contractApplyOrder!list.action',
        method: 'GET',
        params: {
            symbol,
            type,
            page_no
        }
    })
}

// 订单详情
export const _orderDetail = order_no => {
    return request({
        url: '/api/contractApplyOrder!get.action',
        method: 'GET',
        loading: false,
        params: {
            order_no
        }
    })
}

// 永续合约撤单
export const _recallOrder = order_no => {
    return request({
        url: '/api/contractApplyOrder!cancel.action',
        method: 'GET',
        loading: true,
        params: {
            order_no
        }
    })
}

// 永续合约一件撤单
export const _recallOrderBatch = () => {
    return request({
        url: '/api/contractApplyOrder!cancelAll.action',
        method: 'GET',
        loading: true
    })
}

/**      持仓  */

// 持仓
export const _orderListHold = (symbol, type = 'orders', page_no = 1) => {
    return request({
        url: '/api/contractOrder!list.action',
        method: 'GET',
        params: {
            symbol,
            type,
            page_no
        }
    })
}
// 永续合约持仓平仓
export const _orderSell = (params) => {
    return httpJson({
        url: '/api/contractOrder!close.action',
        method: 'GET',
        isLoading: true,
    }, params)
}

// 永续合约一键平仓
export const _orderSellBatch = () => {
    return request({
        url: '/api/contractOrder!closeAll.action',
        method: 'GET',
        loading: true
    })
}

// 永续合约持仓订单详情
export const _orderHoldDetail = order_no => {
    return request({
        url: '/api/contractOrder!get.action',
        loading: false,
        method: 'GET',
        params: {
            order_no
        }
    })
}


/** =============================  交割合约 =======================*/

// 开仓初始化参数
export const _futrueOrderInit = (symbol) => {
    return request({
        url: '/api/futuresOrder!openview.action',
        method: 'GET',
        params: {
            symbol
        }
    })
}

// 开仓
export const _futrueOrder = (params) => {
    return request({
        url: '/api/futuresOrder!open.action',
        method: 'GET',
        loading: true,
        params: {
            symbol: params.symbol,
            session_token: params.session_token,
            direction: params.direction || 'buy',
            amount: params.amount, // 金额
            para_id: params.para_id // 交割周期id
        }
    })
}

// 订单列表
export const _futrueOrderList = (symbol, type = 'orders', page_no = 1) => { // type: orders, hisorders
    return request({
        url: '/api/futuresOrder!list.action',
        method: 'GET',
        params: {
            symbol,
            type,
            page_no
        }
    })
}

// 订单详情
export const _futrueOrderDetail = order_no => {
    return request({
        url: '/api/futuresOrder!get.action',
        method: 'GET',
        loading: false,
        params: {
            order_no
        }
    })
}


/** ================= k line =================== */

// 获取k线
export const _getKline = (symbol, line) => { // 1min, 5min, 15min, 30min, 60min, 4hour, 1day, 1mon, 1week, 1分钟,5分钟,1天,1月
    return request({
        url: 'api/hobi!getKline.action',
        method: 'GET',
        params: {
            symbol,
            line
        }
    })
}


// 分时图
export const _getTrend = (symbol) => {
    return request({
        url: '/api/hobi!getTrend.action',
        method: 'GET',
        params: {
            symbol
        }
    })
}

// 近期交易记录
export const _getTrade = (symbol) => {
    return request({
        url: '/api/hobi!getTrade.action',
        method: 'GET',
        params: {
            symbol
        }
    })
}

// 市场深度数据
